/**
 * "Visual Paradigm: DO NOT MODIFY THIS FILE!"
 * 
 * This is an automatic generated file. It will be regenerated every time 
 * you generate persistence class.
 * 
 * Modifying its content may cause the program not work, or your work may lost.
 */

/**
 * Licensee: DuKe TeAm
 * License Type: Purchased
 */
package smartgrid;

import org.orm.*;
import org.hibernate.Query;
import java.util.List;

import java.io.Serializable;
/**
 * Classe que representa a informação de um contrato.
 */
public class Contract implements Serializable {
	public Contract() {
	}
	
	public static Contract loadContractByORMID(int ID) {
		try {
			PersistentSession session = smartgrid.SmartgridPersistentManager.instance().getSession();
			return loadContractByORMID(session, ID);
		}
		catch (Exception e) {
			return null;
		}
	}
	
	public static Contract getContractByORMID(int ID) {
		try {
			PersistentSession session = smartgrid.SmartgridPersistentManager.instance().getSession();
			return getContractByORMID(session, ID);
		}
		catch (Exception e) {
			return null;
		}
	}
	
	public static Contract loadContractByORMID(int ID, org.hibernate.LockMode lockMode) {
		try {
			PersistentSession session = smartgrid.SmartgridPersistentManager.instance().getSession();
			return loadContractByORMID(session, ID, lockMode);
		}
		catch (Exception e) {
			return null;
		}
	}
	
	public static Contract getContractByORMID(int ID, org.hibernate.LockMode lockMode) {
		try {
			PersistentSession session = smartgrid.SmartgridPersistentManager.instance().getSession();
			return getContractByORMID(session, ID, lockMode);
		}
		catch (Exception e) {
			return null;
		}
	}
	
	public static Contract loadContractByORMID(PersistentSession session, int ID) {
		try {
			return (Contract) session.load(smartgrid.Contract.class, new Integer(ID));
		}
		catch (Exception e) {
			return null;
		}
	}
	
	public static Contract getContractByORMID(PersistentSession session, int ID) {
		try {
			return (Contract) session.get(smartgrid.Contract.class, new Integer(ID));
		}
		catch (Exception e) {
			return null;
		}
	}
	
	public static Contract loadContractByORMID(PersistentSession session, int ID, org.hibernate.LockMode lockMode) {
		try {
			return (Contract) session.load(smartgrid.Contract.class, new Integer(ID), lockMode);
		}
		catch (Exception e) {
			return null;
		}
	}
	
	public static Contract getContractByORMID(PersistentSession session, int ID, org.hibernate.LockMode lockMode) {
		try {
			return (Contract) session.get(smartgrid.Contract.class, new Integer(ID), lockMode);
		}
		catch (Exception e) {
			return null;
		}
	}
	
	public static Contract[] listContractByQuery(String condition, String orderBy) {
		try {
			PersistentSession session = smartgrid.SmartgridPersistentManager.instance().getSession();
			return listContractByQuery(session, condition, orderBy);
		}
		catch (Exception e) {
			return null;
		}
	}
	
	public static Contract[] listContractByQuery(String condition, String orderBy, org.hibernate.LockMode lockMode) {
		try {
			PersistentSession session = smartgrid.SmartgridPersistentManager.instance().getSession();
			return listContractByQuery(session, condition, orderBy, lockMode);
		}
		catch (Exception e) {
			return null;
		}
	}
	
	public static Contract[] listContractByQuery(PersistentSession session, String condition, String orderBy) {
		StringBuffer sb = new StringBuffer("From smartgrid.Contract as Contract");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		try {
			Query query = session.createQuery(sb.toString());
			List list = query.list();
			return (Contract[]) list.toArray(new Contract[list.size()]);
		}
		catch (Exception e) {
			return null;
		}
	}
	
	public static Contract[] listContractByQuery(PersistentSession session, String condition, String orderBy, org.hibernate.LockMode lockMode) {
		StringBuffer sb = new StringBuffer("From smartgrid.Contract as Contract");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		try {
			Query query = session.createQuery(sb.toString());
			query.setLockMode("this", lockMode);
			List list = query.list();
			return (Contract[]) list.toArray(new Contract[list.size()]);
		}
		catch (Exception e) {
			return null;
		}
	}
	
	public static Contract loadContractByQuery(String condition, String orderBy) {
		try {
			PersistentSession session = smartgrid.SmartgridPersistentManager.instance().getSession();
			return loadContractByQuery(session, condition, orderBy);
		}
		catch (Exception e) {
			return null;
		}
	}
	
	public static Contract loadContractByQuery(String condition, String orderBy, org.hibernate.LockMode lockMode) {
		try {
			PersistentSession session = smartgrid.SmartgridPersistentManager.instance().getSession();
			return loadContractByQuery(session, condition, orderBy, lockMode);
		}
		catch (Exception e) {
			return null;
		}
	}
	
	public static Contract loadContractByQuery(PersistentSession session, String condition, String orderBy) {
		Contract[] contracts = listContractByQuery(session, condition, orderBy);
		if (contracts != null && contracts.length > 0)
			return contracts[0];
		else
			return null;
	}
	
	public static Contract loadContractByQuery(PersistentSession session, String condition, String orderBy, org.hibernate.LockMode lockMode) {
		Contract[] contracts = listContractByQuery(session, condition, orderBy, lockMode);
		if (contracts != null && contracts.length > 0)
			return contracts[0];
		else
			return null;
	}
	
	public static java.util.Iterator iterateContractByQuery(String condition, String orderBy) {
		try {
			PersistentSession session = smartgrid.SmartgridPersistentManager.instance().getSession();
			return iterateContractByQuery(session, condition, orderBy);
		}
		catch (Exception e) {
			return null;
		}
	}
	
	public static java.util.Iterator iterateContractByQuery(String condition, String orderBy, org.hibernate.LockMode lockMode) {
		try {
			PersistentSession session = smartgrid.SmartgridPersistentManager.instance().getSession();
			return iterateContractByQuery(session, condition, orderBy, lockMode);
		}
		catch (Exception e) {
			return null;
		}
	}
	
	public static java.util.Iterator iterateContractByQuery(PersistentSession session, String condition, String orderBy) {
		StringBuffer sb = new StringBuffer("From smartgrid.Contract as Contract");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		try {
			Query query = session.createQuery(sb.toString());
			return query.iterate();
		}
		catch (Exception e) {
			return null;
		}
	}
	
	public static java.util.Iterator iterateContractByQuery(PersistentSession session, String condition, String orderBy, org.hibernate.LockMode lockMode) {
		StringBuffer sb = new StringBuffer("From smartgrid.Contract as Contract");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		try {
			Query query = session.createQuery(sb.toString());
			query.setLockMode("this", lockMode);
			return query.iterate();
		}
		catch (Exception e) {
			return null;
		}
	}
	
	public static Contract loadContractByCriteria(ContractCriteria contractCriteria) {
		Contract[] contracts = listContractByCriteria(contractCriteria);
		if(contracts == null || contracts.length == 0) {
			return null;
		}
		return contracts[0];
	}
	
	public static Contract[] listContractByCriteria(ContractCriteria contractCriteria) {
		return contractCriteria.listContract();
	}
	
	public static Contract createContract() {
		return new smartgrid.Contract();
	}
	
	public boolean save() {
		try {
			smartgrid.SmartgridPersistentManager.instance().saveObject(this);
			return true;
		}
		catch (Exception e) {
			return false;
		}
	}
	
	public boolean delete() {
		try {
			smartgrid.SmartgridPersistentManager.instance().deleteObject(this);
			return true;
		}
		catch (Exception e) {
			return false;
		}
	}
	
	public boolean refresh() {
		try {
			smartgrid.SmartgridPersistentManager.instance().getSession().refresh(this);
			return true;
		}
		catch (Exception e) {
			return false;
		}
	}
	
	public boolean evict() {
		try {
			smartgrid.SmartgridPersistentManager.instance().getSession().evict(this);
			return true;
		}
		catch (Exception e) {
			return false;
		}
	}
	
	public boolean deleteAndDissociate() {
		try {
			smartgrid.FaceBookAccount[] lFaceBookAccount_roles = faceBookAccount_role.toArray();
			for(int i = 0; i < lFaceBookAccount_roles.length; i++) {
				lFaceBookAccount_roles[i].contract_role.remove(this);
			}
			smartgrid.Tariff[] lTariff_PreviusTariffs_roles = tariff_PreviusTariffs_role.toArray();
			for(int i = 0; i < lTariff_PreviusTariffs_roles.length; i++) {
				lTariff_PreviusTariffs_roles[i].contract_PreviusTariffs_role.remove(this);
			}
			smartgrid.DataLine[] lDataLiness = dataLines.toArray();
			for(int i = 0; i < lDataLiness.length; i++) {
				lDataLiness[i].setContract(null);
			}
			smartgrid.Annotation[] lAnnotationss = annotations.toArray();
			for(int i = 0; i < lAnnotationss.length; i++) {
				lAnnotationss[i].setContract(null);
			}
			smartgrid.DiaAgregado[] lDiaAgregadoss = diaAgregados.toArray();
			for(int i = 0; i < lDiaAgregadoss.length; i++) {
				lDiaAgregadoss[i].setContract(null);
			}
			smartgrid.MesAgregado[] lMesAgregadoss = mesAgregados.toArray();
			for(int i = 0; i < lMesAgregadoss.length; i++) {
				lMesAgregadoss[i].setContract(null);
			}
			smartgrid.SemanaAgregada[] lSemanaAgregadass = semanaAgregadas.toArray();
			for(int i = 0; i < lSemanaAgregadass.length; i++) {
				lSemanaAgregadass[i].setContract(null);
			}
			return delete();
		}
		catch(Exception e) {
			return false;
		}
	}
	
	public boolean deleteAndDissociate(org.orm.PersistentSession session) {
		try {
			smartgrid.FaceBookAccount[] lFaceBookAccount_roles = faceBookAccount_role.toArray();
			for(int i = 0; i < lFaceBookAccount_roles.length; i++) {
				lFaceBookAccount_roles[i].contract_role.remove(this);
			}
			smartgrid.Tariff[] lTariff_PreviusTariffs_roles = tariff_PreviusTariffs_role.toArray();
			for(int i = 0; i < lTariff_PreviusTariffs_roles.length; i++) {
				lTariff_PreviusTariffs_roles[i].contract_PreviusTariffs_role.remove(this);
			}
			smartgrid.DataLine[] lDataLiness = dataLines.toArray();
			for(int i = 0; i < lDataLiness.length; i++) {
				lDataLiness[i].setContract(null);
			}
			smartgrid.Annotation[] lAnnotationss = annotations.toArray();
			for(int i = 0; i < lAnnotationss.length; i++) {
				lAnnotationss[i].setContract(null);
			}
			smartgrid.DiaAgregado[] lDiaAgregadoss = diaAgregados.toArray();
			for(int i = 0; i < lDiaAgregadoss.length; i++) {
				lDiaAgregadoss[i].setContract(null);
			}
			smartgrid.MesAgregado[] lMesAgregadoss = mesAgregados.toArray();
			for(int i = 0; i < lMesAgregadoss.length; i++) {
				lMesAgregadoss[i].setContract(null);
			}
			smartgrid.SemanaAgregada[] lSemanaAgregadass = semanaAgregadas.toArray();
			for(int i = 0; i < lSemanaAgregadass.length; i++) {
				lSemanaAgregadass[i].setContract(null);
			}
			try {
				session.delete(this);
				return true;
			} catch (Exception e) {
				return false;
			}
		}
		catch(Exception e) {
			return false;
		}
	}
	
	private java.util.Set this_getSet (int key) {
		if (key == smartgrid.ORMConstants.KEY_CONTRACT_FACEBOOKACCOUNT_ROLE) {
			return ORM_faceBookAccount_role;
		}
		else if (key == smartgrid.ORMConstants.KEY_CONTRACT_TARIFF_PREVIUSTARIFFS_ROLE) {
			return ORM_tariff_PreviusTariffs_role;
		}
		else if (key == smartgrid.ORMConstants.KEY_CONTRACT_DATALINES) {
			return ORM_dataLines;
		}
		else if (key == smartgrid.ORMConstants.KEY_CONTRACT_ANNOTATIONS) {
			return ORM_annotations;
		}
		else if (key == smartgrid.ORMConstants.KEY_CONTRACT_DIAAGREGADOS) {
			return ORM_diaAgregados;
		}
		else if (key == smartgrid.ORMConstants.KEY_CONTRACT_MESAGREGADOS) {
			return ORM_mesAgregados;
		}
		else if (key == smartgrid.ORMConstants.KEY_CONTRACT_SEMANAAGREGADAS) {
			return ORM_semanaAgregadas;
		}
		
		return null;
	}
	
	private void this_setOwner(Object owner, int key) {
		if (key == smartgrid.ORMConstants.KEY_CONTRACT_TARIFF_CURRENTTARIF_ROLE) {
			this.Tariff_CurrentTarif_role = (smartgrid.Tariff) owner;
		}
		
		else if (key == smartgrid.ORMConstants.KEY_CONTRACT_SUPPLIER_ROLE) {
			this.supplier_role = (smartgrid.Supplier) owner;
		}
	}
	
	org.orm.util.ORMAdapter _ormAdapter = new org.orm.util.AbstractORMAdapter() {
		public java.util.Set getSet(int key) {
			return this_getSet(key);
		}
		
		public void setOwner(Object owner, int key) {
			this_setOwner(owner, key);
		}
		
	};
	
	private int ID;
	
	private smartgrid.Supplier supplier_role;
	
	private smartgrid.Tariff Tariff_CurrentTarif_role;
	
	private String Number;
	
	private String ClientNif;
	
	private String ClientName;
	
	private String Morada;
	
	private java.util.Set ORM_faceBookAccount_role = new java.util.HashSet();
	
	private java.util.Set ORM_tariff_PreviusTariffs_role = new java.util.HashSet();
	
	private java.util.Set ORM_dataLines = new java.util.HashSet();
	
	private java.util.Set ORM_annotations = new java.util.HashSet();
	
	private java.util.Set ORM_diaAgregados = new java.util.HashSet();
	
	private java.util.Set ORM_mesAgregados = new java.util.HashSet();
	
	private java.util.Set ORM_semanaAgregadas = new java.util.HashSet();
	
	private void setID(int value) {
		this.ID = value;
	}
	
	public int getID() {
		return ID;
	}
	
	public int getORMID() {
		return getID();
	}
	
	/**
	 * Número do contrato.
	 */
	public void setNumber(String value) {
		this.Number = value;
	}
	
	/**
	 * Número do contrato.
	 */
	public String getNumber() {
		return Number;
	}
	
	/**
	 * Nif do titular do contrato.
	 */
	public void setClientNif(String value) {
		this.ClientNif = value;
	}
	
	/**
	 * Nif do titular do contrato.
	 */
	public String getClientNif() {
		return ClientNif;
	}
	
	/**
	 * Nome do titular do contrato.
	 */
	public void setClientName(String value) {
		this.ClientName = value;
	}
	
	/**
	 * Nome do titular do contrato.
	 */
	public String getClientName() {
		return ClientName;
	}
	
	public void setMorada(String value) {
		this.Morada = value;
	}
	
	public String getMorada() {
		return Morada;
	}
	
	private void setORM_FaceBookAccount_role(java.util.Set value) {
		this.ORM_faceBookAccount_role = value;
	}
	
	private java.util.Set getORM_FaceBookAccount_role() {
		return ORM_faceBookAccount_role;
	}
	
	public final smartgrid.FaceBookAccountSetCollection faceBookAccount_role = new smartgrid.FaceBookAccountSetCollection(this, _ormAdapter, smartgrid.ORMConstants.KEY_CONTRACT_FACEBOOKACCOUNT_ROLE, smartgrid.ORMConstants.KEY_FACEBOOKACCOUNT_CONTRACT_ROLE, smartgrid.ORMConstants.KEY_MUL_MANY_TO_MANY);
	
	public void setTariff_CurrentTarif_role(smartgrid.Tariff value) {
		this.Tariff_CurrentTarif_role = value;
	}
	
	public smartgrid.Tariff getTariff_CurrentTarif_role() {
		return Tariff_CurrentTarif_role;
	}
	
	private void setORM_Tariff_PreviusTariffs_role(java.util.Set value) {
		this.ORM_tariff_PreviusTariffs_role = value;
	}
	
	private java.util.Set getORM_Tariff_PreviusTariffs_role() {
		return ORM_tariff_PreviusTariffs_role;
	}
	
	public final smartgrid.TariffSetCollection tariff_PreviusTariffs_role = new smartgrid.TariffSetCollection(this, _ormAdapter, smartgrid.ORMConstants.KEY_CONTRACT_TARIFF_PREVIUSTARIFFS_ROLE, smartgrid.ORMConstants.KEY_TARIFF_CONTRACT_PREVIUSTARIFFS_ROLE, smartgrid.ORMConstants.KEY_MUL_MANY_TO_MANY);
	
	public void setSupplier_role(smartgrid.Supplier value) {
		this.supplier_role = value;
	}
	
	public smartgrid.Supplier getSupplier_role() {
		return supplier_role;
	}
	
	private void setORM_DataLines(java.util.Set value) {
		this.ORM_dataLines = value;
	}
	
	private java.util.Set getORM_DataLines() {
		return ORM_dataLines;
	}
	
	public final smartgrid.DataLineSetCollection dataLines = new smartgrid.DataLineSetCollection(this, _ormAdapter, smartgrid.ORMConstants.KEY_CONTRACT_DATALINES, smartgrid.ORMConstants.KEY_DATALINE_CONTRACT, smartgrid.ORMConstants.KEY_MUL_ONE_TO_MANY);
	
	private void setORM_Annotations(java.util.Set value) {
		this.ORM_annotations = value;
	}
	
	private java.util.Set getORM_Annotations() {
		return ORM_annotations;
	}
	
	public final smartgrid.AnnotationSetCollection annotations = new smartgrid.AnnotationSetCollection(this, _ormAdapter, smartgrid.ORMConstants.KEY_CONTRACT_ANNOTATIONS, smartgrid.ORMConstants.KEY_ANNOTATION_CONTRACT, smartgrid.ORMConstants.KEY_MUL_ONE_TO_MANY);
	
	private void setORM_DiaAgregados(java.util.Set value) {
		this.ORM_diaAgregados = value;
	}
	
	private java.util.Set getORM_DiaAgregados() {
		return ORM_diaAgregados;
	}
	
	public final smartgrid.DiaAgregadoSetCollection diaAgregados = new smartgrid.DiaAgregadoSetCollection(this, _ormAdapter, smartgrid.ORMConstants.KEY_CONTRACT_DIAAGREGADOS, smartgrid.ORMConstants.KEY_DIAAGREGADO_CONTRACT, smartgrid.ORMConstants.KEY_MUL_ONE_TO_MANY);
	
	private void setORM_MesAgregados(java.util.Set value) {
		this.ORM_mesAgregados = value;
	}
	
	private java.util.Set getORM_MesAgregados() {
		return ORM_mesAgregados;
	}
	
	public final smartgrid.MesAgregadoSetCollection mesAgregados = new smartgrid.MesAgregadoSetCollection(this, _ormAdapter, smartgrid.ORMConstants.KEY_CONTRACT_MESAGREGADOS, smartgrid.ORMConstants.KEY_MESAGREGADO_CONTRACT, smartgrid.ORMConstants.KEY_MUL_ONE_TO_MANY);
	
	private void setORM_SemanaAgregadas(java.util.Set value) {
		this.ORM_semanaAgregadas = value;
	}
	
	private java.util.Set getORM_SemanaAgregadas() {
		return ORM_semanaAgregadas;
	}
	
	public final smartgrid.SemanaAgregadaSetCollection semanaAgregadas = new smartgrid.SemanaAgregadaSetCollection(this, _ormAdapter, smartgrid.ORMConstants.KEY_CONTRACT_SEMANAAGREGADAS, smartgrid.ORMConstants.KEY_SEMANAAGREGADA_CONTRACT, smartgrid.ORMConstants.KEY_MUL_ONE_TO_MANY);
	
	/**
	 * Retorna o objecto serializado no formato JSon.
	 */
	public String getJSon() {
		//TODO: Implement Method
		throw new UnsupportedOperationException();
	}
	
	public String toString() {
		return String.valueOf(getID());
	}
	
}
